//登录验证
var countSec = 120;
var INSIDESADE_ADDRESS = "www.exde.com.cn:9998";//外网服务器地址
var isOutService = true;//是否外网访问
jQuery(function() {
//	if(INSIDESADE_ADDRESS == window.location.host){//隐藏
//		document.getElementById("codeDiv").style.display = "none";
//		isOutService = false;
//	}
	document.getElementById("codeDiv").style.display = "none";
	isOutService = false;
	var code;// 验证码
	createCode();
	jQuery(".login").on("click", function() {
		var name = jQuery("#user").val();
		var password =jQuery("#password").val();
		var smsCode =jQuery("#smsCode").val();
		var loginCode = jQuery("#loginCode").val();
		if (loginCode != code) {
			jQuery('#checkSpan').addClass("fa fa-close");
		} else {
			if(isOutService && smsCode == ""){
				alert("验证码不能为空!");
				return;
			}
			jQuery('checkSpan').addClass("fa fa-check");
			var url = window.rootPath + '/ui/login/submit';
			jQuery("input").attr("disabled", true);
			jQuery(".login").attr("disabled", true);
			jQuery(".set").attr("disabled", true);
			jQuery('#loadingImage').show();
			var data = {
				"accountName" : name,
				"password" : sha256_digest(password),
				"smsCode" : smsCode
			}
			jQuery.ajax({
				url : encodeURI(url),
				type : "post",
				dataType : "json",
				data : JSON.stringify(data),
				contentType : "application/json;charset=UTF-8",
				success : function(data) {
					jQuery("input").attr("disabled", false);
					jQuery(".login").attr("disabled", false);
					jQuery(".set").attr("disabled", false);
					jQuery('#loadingImage').hide();
					if (data.code == 0) {
						jQuery('#error').html('用户名或密码错误');
						createCode();
						jQuery("#loginCode").val('');
					} else if (data.code == 1) {
						var picture = data.picture;
						localStorage.setItem('picture', picture);
						localStorage.setItem('name', name);
						localStorage.setItem('userId', data.userId);						
						window.location = window.rootPath + '/ui/index';
					}
					else if (data.code == 3) {
					
						message(data.message);
					}
					else if(data.code == 99){//验证码失败
						jQuery('#error').html(data.message);
					}
				}
			});
		}
	});
	// 登录重置按钮
	jQuery(".lj").on("click", function() {
		window.location = window.ROOTPATH + "/ui/login";
		jQuery("#username").val('');
		jQuery("#key").val('');
	});
	
	jQuery("#SMScodeBtn").on("click",function(){
		var name = jQuery("#user").val();
		if(name == ""){
			alert("请输入账号");
			return;
		}
		jQuery.ajax({
			url : encodeURI(window.rootPath + '/ui/loginmessage/sendMessage/'+name),
			type : 'get',
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				if (data.code == 1) {
					jQuery('#error').html(data.result);
				} else {
					countSec = 120;
					var countdown = setInterval(CountDown, 1000);
				}
			}
		});
	});
	
	// 刷新验证码
	jQuery("#checkCode").on('click', function() {
		createCode();
	})

	function createCode()// 生成验证码
	{
		code = "";
		var codeLength = 4;// 验证码的长度
		var checkCode = document.getElementById("checkCode");
		var selectChar = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c',
				'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p',
				'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');// 所有候选组成验证码的字符，当然也可以用中文的

		for (var i = 0; i < codeLength; i++) {
			var charIndex = Math.floor(Math.random() * 32);
			code += selectChar[charIndex];
		}
		if (checkCode) {
			checkCode.className = "code";
			checkCode.value = code;
		}
	}
});

// 回车登录
document.onkeydown = function(e) {
	var ev = document.all ? window.event : e;
	if (ev.keyCode == 13) {
		jQuery('.login').click();
	}
}

function CountDown() {
	jQuery("#SMScodeBtn").attr("disabled",true);
    $("#SMScodeBtn").val("请等待" + countSec + "秒!");
    if (countSec == 0) {
        $("#SMScodeBtn").attr("disabled",false);
        $("#SMScodeBtn").val("重新发送");
        clearInterval(countdown);
    }
    countSec--;
}

$(".get").click(function(){
	  if(jQuery("#loginForm") && !jQuery("#loginForm").Validform({tiptype:3, showAllError:true}).check(false)){
	        return;
	  };
	  var time=120;//设置时长
//	  $("#phone").attr("disabled", true); 
	  $(".get").attr("disabled", true);           
	  $(".get").val(time+"s后重发");            
	  $(".get").css({"color":"#999","border-color":"#dedede"});
	  var endT;
	  var timer = window.setInterval(function(){           
		endT= --time ;      
	    if(endT>0){                
	      $(".get").val(endT + "s后重发");           
	    }else{                
	      $(".get").val("重新发送");                    
	      $(".get").css({"color":"#17b4eb","border-color":"#17b4eb"});                    
	      $(".get").attr("disabled", false);                      
	      window.clearInterval(timer);           
	    }       
	  },1000);  
	  
	});
//清除COOKIE
//Cookies.remove('JSESSIONID', { path: '' }); 